﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Shamrock.Models
{
    public partial class Message
    {
        public int IndexReplyCount { get { return 5; } }

        public string PostDateWithWeekday
        {
            get
            {
                return PostDate.ToString("yy/MM/dd(dddd)HH:mm", System.Globalization.CultureInfo.CreateSpecificCulture("zh-TW"));
            }
        }

        public string SubjectDefault
        {
            get
            {
                return Subject ?? "無標題";
            }
        }
        public string PosterDefault
        {
            get
            {
                return Poster ?? "無名氏";
            }
        }
        public string CommentDefault
        {
            get
            {
                return Comment ?? "無內文";
            }
        }
        public int Omitted
        {
            get
            {
                var repliesCount = GetRepliesQuery().Count();
                int result = (repliesCount <= IndexReplyCount) ? 0 : repliesCount - IndexReplyCount;
                return result;
            }
        }
        
        public IQueryable<Message> IndexReplies
        {
            get
            {
                var messages = GetRepliesQuery().Skip(Omitted).Take(IndexReplyCount);
                return messages;
            }
        }

        private IQueryable<Message> GetRepliesQuery()
        {
            var messageDb = new ShamrockDataContext();
            var query = from message in messageDb.Messages
                        where message.ParentID.Equals(MessageID)
                        orderby message.PostDate
                        select message;
            return query;
        }
    }
}